<?php
 global $keyword;

	$output ="";
	$output2 ="";
	$keyword="";
if(isset($_REQUEST['txtKey']))
{
	$a="";
	$b="";
	$txtKey =urlencode($_REQUEST['txtKey']);
	$txtKey2 =$_REQUEST['txtKey'];

	$search_term = str_Replace(" ","+",$txtKey);
	$search_term2 = str_Replace(" ","+",$txtKey2);
	$keyword = $txtKey2;


	$url_to_scrape = "http://www.google.com/search?q=site:happy.co.th+$search_term&hl=en&ie=UTF-8&start=0&sa=N";
	//echo $url_to_scrape."<BR>";
	$page_content = file_get_contents($url_to_scrape);
	$pageging = $page_content ;
	//echo $page_content;
	
	//regular expression to parse the page_content	
	//$regex = "/<h2 class=r><a href=\"(.*?)\" class=l>(.*?)\<\/a>/";
	$regex = "/<div class=g>(.*?)<\/div>/";
	$regex2 = "/<div id=navbar class=n>(.*?)<\/div>/";
	
	preg_match_all($regex, $page_content, $matches);
	preg_match_all($regex2, $pageging, $matches2);
	
	foreach ($matches[1] as $key => $result) {
		$b = preg_replace("/<h2 (.*?)>/i","<BR>",$result);
		$b = preg_replace("/<\/h2>/i","",$b);
		$b = preg_replace("/<nobr><a class=fl(.*?)<\/nobr>/i","",$b);

		//$b = preg_replace("/Cached/i","<h4>",$b);
		//$b = preg_replace("/Similar pages/i","<h4>",$b);
		$output .= $b;
	}
	foreach ($matches2[1] as $key => $result2) {
		$a =preg_replace("/<img src=(.*?)><br>/i","",$result2);
		$a =preg_replace("/cellpadding=0/i","cellpadding=3",$a);
		$a =preg_replace("/\/search?/i","./nextpage.php",$a);
		$a =preg_replace("/q=/i","ss=$search_term2&q=",$a);
		$output2 .= $a ;
	}
}
	//echo "<p>".htmlspecialchars($output2)."</p>";	

?>